package com.bvbot.core;

import static com.bvbot.core.BvBotRuntimeException.exit;
import static com.bvbot.core.Logger.log;

import com.bvbot.core.bot.VolatilityBot;
import com.bvbot.core.bot.VolatilityBotConfig;

public class VolatilityLauncher {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		float maxDecrease = getMandatoryFloatParam(args, "maxChangeLimit");
		String maxChangesByMinutes = getMandatoryStringParam(args,
				"maxChangesByMinutes");
		String security = getMandatoryStringParam(args, "security");
		boolean isGold = true;
		if (security.equals("gold")) {
			isGold = true;
		} else if (security.equals("silver")) {
			isGold = false;
		} else {
			BvBotRuntimeException
					.exit("Incorrect parameters given : security not managed : "
							+ security);
		}
		VolatilityBotConfig config = new VolatilityBotConfig();
		config.setMaxChangeLimit(maxDecrease);
		config.initMaxChangesByMinutes(maxChangesByMinutes);
		config.setGold(isGold);
		VolatilityBot bot = new VolatilityBot(config);
		bot.start();
	}

	public static float getMandatoryFloatParam(String[] args, String paramName) {
		Float result = null;
		if ((args == null) || (paramName == null)) {
			BvBotRuntimeException.exit("Incorrect parameters given");
		}
		for (String param : args) {
			String[] split = param.split("=");
			if (split != null) {
				if (split.length == 2) {
					if (split[0].equals(paramName)) {
						String floatString = split[1];
						if (floatString != null) {
							try {
								result = Float.valueOf(floatString);
							} catch (NumberFormatException e) {
								log("Impossible to get a Float from "
										+ floatString + " : " + e.getMessage(),
										e);
							}
						}
					}
				}
			}
		}
		if (result == null) {
			exit("Mandatory param of name " + paramName
					+ " not found in command line parameters.");
		}
		return result;
	}

	public static String getMandatoryStringParam(String[] args, String paramName) {
		String result = null;
		if ((args == null) || (paramName == null)) {
			BvBotRuntimeException.exit("Incorrect parameters given");
		}
		for (String param : args) {
			String[] split = param.split("=");
			if (split != null) {
				if (split.length == 2) {
					if (split[0].equals(paramName)) {
						String paramString = split[1];
						result = paramString;
					}
				}
			}
		}
		if (result == null) {
			exit("Mandatory param of name " + paramName
					+ " not found in command line parameters.");
		}
		return result;
	}

	@SuppressWarnings("unused")
	private static long getMandatoryLongParam(String[] args, String paramName) {
		Long result = null;
		if ((args == null) || (paramName == null)) {
			BvBotRuntimeException.exit("Incorrect parameters given");
		}
		for (String param : args) {
			String[] split = param.split("=");
			if (split != null) {
				if (split.length == 2) {
					if (split[0].equals(paramName)) {
						String longString = split[1];
						if (longString != null) {
							try {
								result = Long.parseLong(longString);
							} catch (NumberFormatException e) {
								log("Impossible to get a Long from "
										+ longString + " : " + e.getMessage(),
										e);
							}
						}
					}
				}
			}
		}
		if (result == null) {
			exit("Mandatory param of name " + paramName
					+ " not found in command line parameters.");
		}
		return result;
	}

}
